﻿using MediatR;
using W.Warehouse.Domain;
using W.Warehouse.Read.Api.Application.Command.BinLocationCommands;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.BinLocationCommandsHandlder
{
    public class WarehouseLocationSelectCommandHandlder : IRequestHandler<WarehouseLocationSelectCommand, ApiResult<List<WarehouseLocation>>>
    {
        private readonly ILogger<WarehouseLocationSelectCommandHandlder> _logger;
        private readonly IWarehouseLocationRepository _warehouseLocationRepository;
        public WarehouseLocationSelectCommandHandlder(IWarehouseLocationRepository warehouseLocationRepository, ILogger<WarehouseLocationSelectCommandHandlder> logger)
        {
            _warehouseLocationRepository = warehouseLocationRepository;
            _logger = logger;
        }
        public async Task<ApiResult<List<WarehouseLocation>>> Handle(WarehouseLocationSelectCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<WarehouseLocation>> result = new ApiResult<List<WarehouseLocation>>();
            try
            {
                _logger.LogInformation("显示库位类型成功");
                var list = await _warehouseLocationRepository.GetAsync(x => x.IsDelete == false);
                result.Data = list;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
